package rp;

import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
import utils.COLOR;
import utils.UTIL;

import java.util.List;
import java.util.Map;
import java.util.Set;


public class LSNMain implements Listener {

    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
        e.setJoinMessage(null);
        UTIL.NOTICE(COLOR.GOLD.getValue() + "玩家 " + COLOR.WHITE.getValue() + e.getPlayer().getName() + COLOR.GOLD.getValue() + " 上线了.");
    }

    @EventHandler
    public void onQuit(PlayerQuitEvent e) {
        e.setQuitMessage(null);
        UTIL.NOTICE(COLOR.GOLD.getValue() + "玩家 " + COLOR.WHITE.getValue() + e.getPlayer().getName() + COLOR.GOLD.getValue() + " 下线了.");
    }

    @EventHandler
    public void onChat(AsyncPlayerChatEvent e) {
        for (Map<String, String> temp : COLOR.getAll())
            e.setMessage(e.getMessage().replace(temp.get("text"), temp.get("value")));
        String world = "";
        switch (e.getPlayer().getLocation().getWorld().getName()) {
            case "world":
                world = COLOR.GRAY.getValue() + "[" + COLOR.LEAVES.getValue() + "生存" + COLOR.GRAY.getValue() + "]";
                break;
            case "world_nether":
                world = COLOR.GRAY.getValue() + "[" + COLOR.BLOOD.getValue() + "下界" + COLOR.GRAY.getValue() + "]";
                break;
            case "world_the_end":
                world = COLOR.GRAY.getValue() + "[" + COLOR.PURPLE.getValue() + "末地" + COLOR.GRAY.getValue() + "]";
                break;
        }
        //String level = COLOR.GRAY.getValue() + "[" + COLOR.GREEN.getValue() + e.getPlayer().getLevel() + COLOR.WHITE.getValue() + "级" + COLOR.GRAY.getValue() + "]";
        String name = COLOR.GRAY.getValue() + "<" + COLOR.WHITE.getValue() + "%1$s" + COLOR.GRAY.getValue() + ">";
        String content = COLOR.DEFAULT.getValue() + "%2$s";
        e.setFormat(world + name + content);
    }

    @EventHandler
    public void onClick(InventoryClickEvent e) {
        Inventory inv = e.getInventory();
        Player player = (Player) e.getWhoClicked();
        net.minecraft.server.v1_15_R1.ItemStack nms = CraftItemStack.asNMSCopy(e.getCurrentItem());
        if (nms.hasTag()) {
            player.closeInventory();
            if (e.isLeftClick() && nms.getTag().hasKey("Click"))
                UTIL.EXECUTE(player, nms.getTag().getString("Click"));
            if (e.isLeftClick() && nms.getTag().hasKey("Left"))
                UTIL.EXECUTE(player, nms.getTag().getString("Left"));
            if (e.isRightClick() && nms.getTag().hasKey("Right"))
                UTIL.EXECUTE(player, nms.getTag().getString("Right"));
            e.setCancelled(true);
        }
    }

    @EventHandler
    public void onPickup(EntityPickupItemEvent e) {
        if (e.getEntity() instanceof Player)
            if (!((Player) e.getEntity()).isSneaking())
                e.setCancelled(true);
    }

    @EventHandler
    public void onDeath(PlayerDeathEvent e) {

    }
}
